package com.jeepig.web.controller.evaluation;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jeepig.evaluation.domain.Evaluate;
import com.jeepig.evaluation.service.IEvaluateService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jeepig.common.annotation.Log;
import com.jeepig.common.core.controller.BaseController;
import com.jeepig.common.core.domain.AjaxResult;
import com.jeepig.common.enums.BusinessType;
import com.jeepig.evaluation.domain.ComEvaluation;
import com.jeepig.evaluation.service.IComEvaluationService;
import com.jeepig.common.utils.poi.ExcelUtil;
import com.jeepig.common.core.page.TableDataInfo;

/**
 * 综合评价结果Controller
 *
 * @author ruoyi
 * @date 2025-11-17
 */
@RestController
@RequestMapping("/evaluation/comEvaluation")
public class ComEvaluationController extends BaseController
{
    @Autowired
    private IComEvaluationService comEvaluationService;

    @Autowired
    private IEvaluateService evaluateService;

    /**
     * 查询综合评价结果列表
     */
    @PreAuthorize("@ss.hasPermi('evaluation:comEvaluation:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComEvaluation comEvaluation)
    {
        startPage();
        List<ComEvaluation> list = comEvaluationService.selectComEvaluationList(comEvaluation);
        return getDataTable(list);
    }

    /**
     * 导出综合评价结果列表
     */
    @PreAuthorize("@ss.hasPermi('evaluation:comEvaluation:export')")
    @Log(title = "综合评价结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComEvaluation comEvaluation)
    {
        List<ComEvaluation> list = comEvaluationService.selectComEvaluationList(comEvaluation);
        ExcelUtil<ComEvaluation> util = new ExcelUtil<ComEvaluation>(ComEvaluation.class);
        util.exportExcel(response, list, "综合评价结果数据");
    }

    /**
     * 获取综合评价结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('evaluation:comEvaluation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(comEvaluationService.selectComEvaluationById(id));
    }

    /**
     * 新增综合评价结果
     */
    @PreAuthorize("@ss.hasPermi('evaluation:comEvaluation:add')")
    @Log(title = "综合评价结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComEvaluation comEvaluation)
    {
        return toAjax(comEvaluationService.insertComEvaluation(comEvaluation));
    }

    /**
     * 新增综合评价结果
     * @param memberId
     * @param evaluateId
     * @param score
     * @param content
     * @return
     */
    @Log(title = "综合评价结果", businessType = BusinessType.INSERT)
    @GetMapping("/save/{memberId}/{evaluateId}")
    public AjaxResult add(@PathVariable("memberId") Long memberId,
                          @PathVariable("evaluateId")Long evaluateId,
                          String score, String content){
        ComEvaluation comEvaluation = comEvaluationService.selectComEvaluationByMemberId(memberId, evaluateId);
        if(comEvaluation == null){
            comEvaluation = new ComEvaluation();
            comEvaluation.setMemberId(memberId);
            comEvaluation.setEvalItemId(evaluateId);
            Evaluate evaluate = evaluateService.selectEvaluateById(String.valueOf(evaluateId));
            if(evaluate != null){
                comEvaluation.setEvalItemName(evaluate.getName());
                comEvaluation.setSn(evaluate.getSn());
            }
            comEvaluation.setScore(score);
            comEvaluation.setContent(content);
            return toAjax(comEvaluationService.insertComEvaluation(comEvaluation));
        }else{
            comEvaluation.setScore(score);
            comEvaluation.setContent(content);
            return toAjax(comEvaluationService.updateComEvaluation(comEvaluation));
        }
    }

    /**
     * 修改综合评价结果
     */
    @PreAuthorize("@ss.hasPermi('evaluation:comEvaluation:edit')")
    @Log(title = "综合评价结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComEvaluation comEvaluation)
    {
        return toAjax(comEvaluationService.updateComEvaluation(comEvaluation));
    }

    /**
     * 删除综合评价结果
     */
    @PreAuthorize("@ss.hasPermi('evaluation:comEvaluation:remove')")
    @Log(title = "综合评价结果", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(comEvaluationService.deleteComEvaluationByIds(ids));
    }
}
